$(document).ready(function () {
// $("#address").text(productName);
//     initSession_ID(function () {
        //领取分享券
        if(SharedCoupon && IS_BIND) {
                    location.replace("./receive_success_toUse.html" + shareCouponParams);
            return false;
        }
        //正常注册流程
        if(!IS_BIND) {
            //领取卡券流程，判断传值是否为领取卡券
            if(SharedCoupon) {  //是否分享券编码（是否存在）
                //（调取接口查看当前是否有剩余卡券）,如果有则显示正常短信验证领取页，没有则显示已领取完毕
                $("title").text("领券");
                $("#register").text("领取");
            } else {
                $("#register").text("立即注册");
            }
        } else {
            $("title").text("修改手机号码");
            $("#register").text("立即修改");
        };
        var headImg = sessionStorage.getItem("user_head_img");
        if(headImg) {
            $("#user_head_img").attr("src",headImg);
        }
        var infoPath = "user/info/get";
        var params = {
            "SESSION_ID":SESSION_ID
        };
        request(infoPath,params,function (res) {
            $("#gender").attr("src",res.BODY.GENDER == 1 ? "../assets/img/ico_nan.png" : "../assets/img/ico_nv.png");
            $("#nickName").text(res.BODY.USER_NAME ? res.BODY.USER_NAME : "未知");
            $("#address").text(res.BODY.ADDR ? res.BODY.ADDR : "未知");
        })
    // })
});
function canGainVertify(phone) {
    if(phoneCHeck(phone)) {
        $(".gain-vertif").css("background","#008065");
        $(".gain-vertif").attr("disabled",false);
        var phone = $("#phone").val();
        sessionStorage.setItem("registerPhone",phone);
    } else {
        $(".gain-vertif").css("background","#666");
        $(".gain-vertif").attr("disabled",true);
    }
}
function showRemove(dom) {
    if(!dom.value) {
        $(".clear-number").css("display","none");
    } else {
        $(".clear-number").css("display","inline-block");
        canGainVertify(dom.value)
    }
}
function clearPhone() {
    $("#phone").val('');
    $(".clear-number").css("display","none");
    var phone = $("#phone").val()
    canGainVertify(phone);
}
function gainVertif(dom) {
    var phoneNum = sessionStorage.getItem("registerPhone");
    var smpath = "user/smscode/changePhone";  //获取验证码
    var smparams = {
        "PHONE": phoneNum
    }
    request(smpath,smparams,function (res) {
        // var flg = /^0+$/ig.test(res.RSPCD); //全零true
        if(res.RSPCD == "USR100012") {
            showErroMsg("该手机号已注册，不能重复注册！",function(){
                $(".showErrorMassage").remove();
            });
        } else if(res.RSPCD == "USR100001") {
            showErroMsg("手机号码格式不正确！",function(){
                $(".showErrorMassage").remove();
            });
        } else if(res.RSPCD == "USR100002") {
            showErroMsg("系统繁忙，短信下发失败！",function(){
                $(".showErrorMassage").remove();
            });
        } else if(res.RSPCD == "USR100003") {
            showErroMsg("手机验证码请求太频繁，请稍后再试！",function(){
                $(".showErrorMassage").remove();
            });
        } else if(res.RSPCD == "USR100009") {
            showErroMsg("该会员已冻结！",function(){
                $(".showErrorMassage").remove();
            });
        } else if(res.RSPCD == "USR100021") {
            showErroMsg("该手机号已绑定其他微信账号！",function(){
                $(".showErrorMassage").remove();
            });
        }  else if(res.RSPCD == "400003") {
            showErroMsg("系统繁忙！",function(){
                $(".showErrorMassage").remove();
            });
        } else {
            var time = 60;
            var timer = setInterval(function () {
                $(dom).text(--time + "s后重新获取").attr("disabled",true);
                if(time < 0) {
                    clearInterval(timer);
                    $(dom).text("重新获取");
                    $(dom).attr("disabled",false);
                }
            },1000);
        }
    })
    /*var phone = $("#phone").val();
    sessionStorage.setItem("registerPhone",phone);*/
}
function checkCanRegister() {
    var Verif = $("#verification").val();
    var agree = $("#agreement_check")[0].checked; //默认false，选中时返回false，后改为true
    // agree = flag ?  !agree : agree;
    if(Verif.length > 0 && agree) {
        $("#register").attr("disabled",false);
        $("#register").css("background","#008065");
    } else {
        $("#register").attr("disabled",true);
        $("#register").css("background","#666");
    }
}
$("#agreement_check").on("click",function () {
    checkCanRegister()
})

function submitPhone(form) {
    var phone = sessionStorage.getItem("registerPhone");
    var vertification = form.vertification.value;
    //验证码不正确请重新输入
    /*if() {

    }*/
    var smsCodePath = "user/phone/bind";  /*换绑接口*/
    var smsCodeParams = {
        "PHONE":phone,
        "SMS_CODE":vertification
    }
    request(smsCodePath,smsCodeParams,function (res) {
        var flg = /^0+$/ig.test(res.RSPCD); //全零true
        if(res.RSPCD == "USR100004") {
            return showErroMsg("验证码错误！",function(){
                $(".showErrorMassage").remove();
            });
        } else if(res.RSPCD == "USR100012") {
            return showErroMsg("该手机号已注册，不能重复注册！",function(){
                $(".showErrorMassage").remove();
            });
        }/* else if(!flg) {
            return showErroMsg("验证出错!");
        }*/
        /*sessionStorage.setItem("SESSION_ID",res.BODY.SESSION_ID);
        var isRegister = res.BODY.IS_REGISTER;
        if(isRegister) return showErroMsg("该手机号已绑定，请更换手机号！");*/
        /*var ticket = res.BODY.TICKET;
        var password = "E8151166BDD1264FD75B080E9025AA2E";
        var bindPath = "wx/user/bind";
        var binParams = {
            "SESSION_ID":SESSION_ID,
            "TICKET":ticket,
            "PASSWORD_MD5":password
        };*/
       /* request(bindPath,binParams,function (res) {
            var flg = /^0+$/ig.test(res.RSPCD); //全零true*/
            //领取分享券
            if($("#register").text() == "领取") {
                if(!flg) return showErroMsg("领取失败，请重新领取！",function(){
                $(".showErrorMassage").remove();
            });
               /* var orderShareGetPath = "order/share/get";
                var orderShareGetParams = {
                    "SHARE_ID":SHARE_ID,
                    "STORE_ID":STORE_ID
                };
                request(orderShareGetPath,orderShareGetParams,function (res) {
                    var flg = /^0+$/ig.test(res.RSPCD); //全零true
                    if(!flg) {
                        return showErroMsg("分享券已失效，返回首页!",function () {
                            location.replace("./index.html");
                        });
                    } else {*/
                        location.replace("./receive_success_toUse.html" + shareCouponParams);
                /*    }
                })*/
                return false;
            } else if($("#register").text() == "立即注册") {
                if(!flg) return showErroMsg("注册失败，请重新注册！",function(){
                $(".showErrorMassage").remove();
            });
                $(".modal-text").text("恭喜你成为会员");
            } else if($("#register").text() == "立即修改") {
                if(!flg) return showErroMsg("修改失败，请重新修改！",function(){
                $(".showErrorMassage").remove();
            });
                $(".modal-text").text("手机号码修改成功");
            }
            $(".fixed-panal").css('display',"block");
            var time = 5;
            var timer = setInterval(function () {
                $(".modal-footer").text(--time + "秒后自动跳至首页")
                if(time <= 0) {
                    location.href= './index.html';
                    clearInterval(timer);
                }
            },1000);
        // });
    })
}